<template>
  <div class="article-main">
    <el-card class="margin-30" shadow="never">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <!-- 卡片内容 -->
      <el-form ref="form" :model="form" label-width="80px" size="mini">
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item label="作者">
              <el-input v-model="form.author" placeholder="作者" />
            </el-form-item>
          </el-col>

          <el-col :span="6" :offset="0">
            <el-form-item label="标题">
              <el-input v-model="form.title" placeholder="标题" />
            </el-form-item>
          </el-col>

          <el-col :span="6" :offset="0">
            <el-form-item label="编辑类型">
              <!-- editorType (integer, optional): 编辑器类型: 0 富文本; 1 markdown , -->
              <el-select v-model="form.editorType" style="width: 100%;" placeholder="编辑器类型" clearable>
                <el-option label="富文本" :value="0" />
                <el-option label="markdown" :value="1" />
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="18">
            <el-form-item>
              <el-button @click="resetForm">重置</el-button>
              <el-button type="primary" @click="getSearchlist">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 文章列表 -->
    <el-card shadow="never" class="margin-30">
      <div slot="header">
        <el-button type="primary" size="mini" @click="goAddArticle">新增</el-button>
      </div>
      <!-- card body -->
      <el-table v-loading="loading" :data="articleList" border element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.5)" style="width: 100%">
        <el-table-column label="#" type="index" width="50" align="center" />
        <el-table-column prop="title" label="文章标题" align="center" />
        <el-table-column label="展示图片" width="130" align="center">
          <template slot-scope="scope">
            <img width="100" height="100" :src="scope.row.coverImg" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间" align="center" />
        <el-table-column prop="author" label="文章作者" width="160" align="center" />
        <el-table-column label="文章是否显示" width="150" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0"
              @change="articleShowStatusChange(scope.row.isShow, scope.row.id)" />
          </template>
        </el-table-column>
        <el-table-column label="编辑器类型" width="150" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.editorType === 0" type="primary" size="mini" plain>富文本</el-button>
            <el-button v-if="scope.row.editorType === 1" type="primary" size="mini" plain>markdown</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <router-link :to="`AddArticle?id=${scope.row.id}`"><el-button type="text" size="mini"
                icon="el-icon-view">编辑</el-button></router-link>
            <el-button type="text" size="mini" style="color: #f00;" @click="delActicleById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination style="margin-top: 30px; text-align: center;" :current-page="start" :page-sizes="[10, 20, 30]"
        :page-size="limit" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-card>
  </div>
</template>

<script>
import { findArticles, changeShowStatus, del } from '@/api/content/article'
import { filterNullStr } from '@/utils'
import mix from '@/mixins/index'
export default {
  name: 'ArticleList',
  mixins: [mix],
  data() {
    return {
      loading: false,
      form: {},
      articleList: []
    }
  },

  mounted() {
    this.getArticleList()
  },

  methods: {
    // 删除文章
    delActicleById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await del(id)
        const { success, message } = res
        if (success) {
          this.$message.success('删除成功')
          // 删除成功重新调用数据
          this.getArticleList()
        } else {
          this.$message.error(message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 改变文章显示状态
    async articleShowStatusChange(e, id) {
      const res = await changeShowStatus({
        isShow: e,
        id
      })
      const { success, message } = res
      if (success) {
        this.$message.success('状态修改成功')
      } else {
        this.$message.error(message)
      }
    },
    // 新增文章
    goAddArticle() {
      this.$router.push({ path: 'AddArticle' })
    },
    // 编辑文章
    goEditArticle(id) {
      this.$router.push({ path: 'AddArticle', params: { id } })
    },

    // 重置表单
    resetForm() {
      //  this.$refs  //获取实例对象
      // vue2 $options 可以获取整个页面的初始值
      // console.log(this.$options.data());

      this.form = this.$options.data().form
      this.start = 1
      this.getArticleList()
    },
    // 按条件搜索
    getSearchlist() {
      this.start = 1
      this.getArticleList()
    },

    //  获取文章列表
    async getArticleList() {
      try {
        // 开启进度条
        this.loading = true

        // 过滤表单空值参数
        const parmsa = filterNullStr(this.form)

        // 获取文章列表
        const res = await findArticles(this.start, this.limit, parmsa)
        const { success, data: { rows, total }, message } = res
        console.log(res)

        if (success) {
          this.articleList = rows
          this.total = total
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        this.$message.error('请求出错')
      } finally {
        // 关闭进度条
        this.loading = false
      }
    },

    // 使用debounce函数延迟请求
    // 分页
    handleSizeChange(val) {
      this.limit = val
      this.start = 1
      this.getArticleList()
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.start = val
      this.getArticleList()
      // console.log(`当前页: ${val}`);
    }

  }
}
</script>

<style lang="scss" scoped></style>
